package com.rambo.baselib.fraction;

import com.rambo.baselib.ResourceTable;
import com.rambo.baselib.bean.LoadDataConfig;
import com.rambo.baselib.mvp.BasePresenter;
import com.rambo.baselib.mvp.IView;
import com.rambo.baselib.util.Loading;
import com.yan.zrefreshview.ZRefreshView;
import ohos.agp.components.*;
import ohos.app.Context;


/***********************************************************************
 * <p>@description: 下拉刷新和上拉 加载更多
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/13 16:07
 **********************************************************************/
public abstract class BaseRefreshListFraction<V extends IView, P extends BasePresenter<V>> extends BaseFraction<V, P> {
    protected LoadDataConfig loadDataConfig = new LoadDataConfig();
    protected ZRefreshView refreshView;
    protected ListContainer listContainer;
    private boolean enableLoadMore;

    public BaseRefreshListFraction(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_componet_refresh_list;
    }

    @Override
    protected void initView(Component component) {
        super.initView(component);
        refreshView = (ZRefreshView) component.findComponentById(ResourceTable.Id_zrefresh_view);
        enableLoadMore = enableLoadMore();
        refreshView.setEnableLoadMore(enableLoadMore);
        refreshView.setEnableRefresh(enableRefresh());
        listContainer = (ListContainer) component.findComponentById(ResourceTable.Id_list_container);
        listContainer.setItemProvider(getItemProvider());
        refreshView.setOnRefreshListener(new ZRefreshView.RefreshListener() {
            @Override
            public void onPullRefreshing() {
                loadDataConfig.curPage = 0;
                loadDataConfig.isLoadingMore = false;
                loadData(loadDataConfig);
            }
        });
        refreshView.setLoadMoreListener(new ZRefreshView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadDataConfig.isLoadingMore = true;
                loadData(loadDataConfig);
            }
        });

    }

    @Override
    protected void fillData() {
        super.fillData();
        if (autoLoad()) {
            loading.updateLoadState(Loading.LoadingState.LOADING);
            loadData(loadDataConfig);
        }
    }

    protected boolean autoLoad() {
        return true;
    }

    protected abstract BaseItemProvider getItemProvider();

    protected abstract void loadData(LoadDataConfig refreshConfig);

    /**
     * 是否可以下拉刷新
     *
     * @return
     */
    protected boolean enableRefresh() {
        return true;
    }

    /**
     * 是否可以底部加载更多
     *
     * @return
     */
    protected boolean enableLoadMore() {
        return true;
    }

    /**
     * 数据加载完成后，需要设置标记
     *
     * @param success
     * @param hasMore
     */
    protected void loadDataFinish(boolean success, boolean hasMore) {
        if (success) {
            loading.updateLoadState(Loading.LoadingState.SUCCESS);
        }
        if (!hasMore) {
            refreshView.setEnableLoadMore(false);
        }
        if (loadDataConfig.isLoadingMore) {
            refreshView.finishLoadMore();
            if (success) {
                loadDataConfig.curPage += 1;
            }
        } else {
            refreshView.finishRefreshing();
            if (hasMore && enableLoadMore) {
                refreshView.setEnableLoadMore(true);
            }
        }
    }

}
